<?php
namespace app\admin\controller;
use app\admin\code\BaseCode;
use app\admin\logic\TagLogic;
use app\admin\model\TagModel;
use app\admin\util\Page;
use app\admin\util\Response;
use app\admin\validate\TagValidate;
use think\Request;

class Tag extends Base
{
    public function index(){
        if($this->request->isAjax()){
            $model = new TagModel();
            $list = $model->selectData([]);
            $size = input('limit',10,'intval');
            $page = input('page',1,'intval');
            Response::json(BaseCode::SUCCESS,'',Page::return_page_data($list,$size,$page));
        }else{
            return view();
        }
    }
    public function del(){
        $param = [];
        $param['id'] = input('id',0,'intval');
        $logic = new TagLogic($param);
        if($logic->del(new TagValidate(new TagModel()))){
            Response::json(BaseCode::SUCCESS);
        }else{
            Response::json($logic->err['code'],$logic->err['msg']);
        }
    }
    public function detail(){
        $id = input('id',0,'intval');
        $model = new TagModel();
        $vo = $model->findData(['id'=>$id]);
        $this->assign('vo',$vo);
        $this->assign('is_update',1);
        return view('oppt');
    }
    public function update(){
        $param = [];
        $param['id'] = input('id',0,'intval');
        $param['name'] = input('name','','htmlspecialchars');
        $param['sort'] = input('sort',0,'intval');
        $logic = new TagLogic($param);
        if($logic->update(new TagValidate(new TagModel()))){
            Response::json(BaseCode::SUCCESS);
        }else{
            Response::json($logic->err['code'],$logic->err['msg']);
        }
   }
   public function add(){
        if(Request::instance()->isAjax()){
            $param = [];
            $param['name'] = input('name','','htmlspecialchars');
            $param['sort'] = input('sort',0,'intval');
            $logic = new TagLogic($param);
            if($logic->add(new TagValidate(new TagModel()))){
                Response::json(BaseCode::SUCCESS);
            }else{
                Response::json($logic->err['code'],$logic->err['msg']);
            }
        }else{
            $this->assign('is_insert',1);
            return view('oppt');
        }
   }
}